package util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class XmlParser {
	private File file = new File("savedResults.xml");
	private BufferedWriter output;
	private BufferedWriter outputClear;
	private FileReader input;
	XmlPullParserFactory factory;
	XmlPullParser xpp;
	LinkedList<savedResultWrapper> results;

	public XmlParser(File file) {
		this.file = file;
		initializeOutput();
	}
	
	public void addEntry(savedResultWrapper newSavedResult){
		results = readAll();
		clearFile();
		String result = "<saves>\r\n";
		Iterator<savedResultWrapper> itr = results.iterator();
		while (itr.hasNext()) {
			savedResultWrapper savedResult = itr.next();
			result += savedResult.toString();
		}
		newSavedResult.setId(results.getLast().id + 1);
		result += newSavedResult.toString();
		result += "</saves>";
		write(result);
	}

	private void initializeInput() {
		try {
			input = new FileReader(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		try {
			factory = XmlPullParserFactory.newInstance();
			xpp = factory.newPullParser();
			xpp.setInput(input);
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
	}
	
	private void initializeOutput() {
		try {
			output = new BufferedWriter(new FileWriter(file, true));
			output.flush();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	private void write(String entry) {
		try {
			output.write(entry);
			output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void remove(int id) {
		results = readAll();
		String result = "<saves>\r\n";
		Iterator<savedResultWrapper> itr = results.iterator();
		while (itr.hasNext()) {
			savedResultWrapper savedResult = itr.next();
			if (savedResult.id != id) {
				result += savedResult.toString();
			}
		}
		result += "</saves>";
		clearFile();
		write(result);
	}
	
	public savedResultWrapper getById(int id) {
		results = readAll();
		Iterator<savedResultWrapper> itr = results.iterator();
		while (itr.hasNext()) {
			savedResultWrapper savedResult = itr.next();
			if (savedResult.id == id) {
				return savedResult;
			}
		}
		return null;
	}

	private void clearFile(){
		try {
			outputClear = new BufferedWriter(new FileWriter(file, false));
			outputClear.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public LinkedList<savedResultWrapper> readAll() {
		initializeInput();
		String startTag, endTag;
		savedResultWrapper result = null;
		results = new LinkedList<savedResultWrapper>();
		int eventType = 0;
		try {
			eventType = xpp.getEventType();
			do {
				if (eventType == XmlPullParser.START_DOCUMENT) {
				}else if (eventType == XmlPullParser.START_TAG) {
					startTag = xpp.getName();
					if (startTag.equals("save")) {
						result = new savedResultWrapper();
					} else if (startTag.equals("id")) {
						result.setId(Integer.parseInt(xpp.nextText()));
					} else if (startTag.equals("targetId")) {
						result.setTargetId(Integer.parseInt(xpp.nextText()));
					} else if (startTag.equals("name")) {
						result.setName(xpp.nextText());
					} else if (startTag.equals("values")) {
						result.populateMap(xpp.nextText());
					}
				} else if (eventType == XmlPullParser.END_TAG) {
					endTag = xpp.getName();
					if (endTag.equals("save")) {
						results.add(result);
					}
				}else if (eventType == XmlPullParser.END_DOCUMENT) {
					//return results;
					break;
				}
				eventType = xpp.next();
			} while (eventType != XmlPullParser.END_DOCUMENT);
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return results;
	}
}
